<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>彪哥的地图</title>
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  <style>
    html,
    body,
    #container {
      width: 100%;
      height: 100%;
    }
    .Inglat{
      color: #0288d1;
    }
    .Inglat2{
      color: #336633;
    }
  </style>
</head>

<body>
  <div id="container"></div>

  <div class = "info">
    <h4>当前地图显示范围</h4>
    <p class = "Inglat2" >NorthEast坐标：<span id = "ne" class = "Inglat"> </span></p>
    <p class = "Inglat2" >SouthWest坐标：<span id = "sw" class = "Inglat"> </span></p>
  </div>


  <div class="input-card" style="width:24rem;">
    <h4>设置地图当前行政区</h4>
    <div class="input-item">
      <input id='city-name' placeholder="长沙市" type="text" style="margin-right:1rem;"><button class="btn" id="query">去指定城市</button>
    </div>


    <h4>限制地图显示范围</h4>
    <div class = "input-item">
      <button class = "btn" id = "lock-bounds"> 限定地图显示范围 </button>
    </div>
    <div class = "input-item">
      <button class = "btn" id = "unlock-bounds"> 未限定地图显示范围 </button>
    </div>
  </div>

  <script src="https://webapi.amap.com/maps?v=1.4.15&key=5ed4e9faab53f848f70b155242204142"></script>
  <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
  <script>
    var map = new AMap.Map('container', {
      resizeEnable: true,
      center: [116.405285,39.904989]
    });

    //根据cityname、adcode、citycode设置地图位置
    function gotoCity() {
      var val = document.querySelector('#city-name').value; //可以是cityname、adcode、citycode
      if (!val) {
        val = "长沙市";
      }
      map.setCity(val);
      log.info(`已跳转至${val}`);
    }
    gotoCity();

    function logMapBounds(){
      var bounds = map.getBounds();
      document.querySelector("#ne").innerText = bounds.northeast.toString();
      document.querySelector("#sw").innerText = bounds.southwest.toString();
    }
    logMapBounds();

    map.on("moveend",logMapBounds);
    map.on("zoomend",logMapBounds);


    function logMapInfo(){
      var limitBounds = map.getLimitBounds();
      if(limitBounds){
        document.querySelector("#ne").innerText = bounds.northeast.toString();
        document.querySelector("#sw").innerText = bounds.southwest.toString();
      }else{
        document.querySelector("#ne").innerText = document.querySelector("#sw").innerText = "咩限定"; 
      }
    }

    function lockMapBounds(){
      var bounds = map.getBounds();
      map.setLimitBounds(bounds);
      logMapInfo();
    }

    function unlockMapBounds(){
      map.clearLimitBounds();
      logMapInfo();
    }

    lockMapBounds();
    logMapInfo();
    
    document.querySelector("#lock-bounds").onclick = lockMapBounds;
    document.querySelector("#unlock-bounds").onclick = unlockMapBounds;
    //绑定查询点击、回车事件
    document.querySelector('#query').onclick = gotoCity;
    document.querySelector('#city-name').onkeydown = function(e) {
      if (e.keyCode === 13) {
        gotoCity();
        return false;
      }
      return true;
    };
  </script>
</body>

</html>